<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="flex-1" :model="formState" :colon="false" :label-col="{ style: { width: '70px' } }">
        <a-form-item name="zjm" class="w-[350px]">
          <a-input v-model:value="formState.zjm" placeholder="区域编号/区域名称" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <a-space class="mb-3">
        <a-button type="primary" @click="onClickEdit()">新增</a-button>
        <!-- <a-button @click="onExport">导出</a-button> -->
      </a-space>
      <BaseTable :loading="loading" column-code="00001052" fill :data-source="tableData" bordered :pagination="pagination" @change="onPageChange">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" @click="onClickEdit(record)"> 修改 </a-button>
            <a-button type="link" size="small" @click="onClickProtect(record)"> 维护人员 </a-button>
            <ConfirmModal description="确定删除?" @confirm="onConfirm(record)">
              <a-button type="text" danger size="small"> 删除 </a-button>
            </ConfirmModal>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="editOpen" :row-data="rowData" @success="getData" />
    <ProtectModal v-model:open="protectOpen" :row-data="rowData" />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import SearchContainer from '@/components/SearchContainer'
import BaseTable from '@/components/BaseTable'
import ConfirmModal from '@/components/ConfirmModal'
import Edit from './components/Edit.vue'
import ProtectModal from './components/ProtectModal.vue'

const rowData = ref({})
const editOpen = ref(false)
const protectOpen = ref(false)
const formRef = ref(null)
const formState = ref({})

const { loading, tableData, getData, pagination } = useTable({
  url: '/operation/opex/getWardInfoList',
  immediate: true,
  params: formState.value
})

const onClickEdit = (row = {}) => {
  editOpen.value = true
  rowData.value = { ...row }
}

// 维护人员
const onClickProtect = (row) => {
  rowData.value = { ...row }
  protectOpen.value = true
}

const onConfirm = async (row) => {
  const { code, msg } = await post('/operation/opex/doDelWardInfo', { id: row.id })
  code ? message.error(msg) : message.success(msg)
  getData()
}
</script>
